package com.whw.fy.service.impl;

import com.whw.fy.entity.*;
import com.whw.fy.entity.count.CountAllStudentStatus;
import com.whw.fy.entity.count.CountAllTeachingManagementTimesStatistics;
import com.whw.fy.entity.count.PaymentAmountStatistics;
import com.whw.fy.entity.count.TeacherWorkloadStatistics;
import com.whw.fy.mapper.*;
import com.whw.fy.service.CountService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.ref.PhantomReference;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

@Service
public class CountServiceImpl implements CountService {
    @Resource
    private ScheduleMapper scheduleMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private TechnicalTypeMapper technicalTypeMapper;
    @Resource
    private PayMapper payMapper;
    @Resource
    private PurchaseCourseRecordsMapper purchaseCourseRecordsMapper;
    @Resource
    private ClazzTypeMapper clazzTypeMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private PlansMapper plansMapper;
    @Resource
    private PlansclazzMapper plansclazzMapper;
    @Resource
    private CourseSummaryMapper courseSummaryMapper;
    @Resource
    private TeachSummaryMapper teachSummaryMapper;
    @Resource
    private TeachingDiscussionMapper teachingDiscussionMapper;
    @Resource
    private TeachingTodoMapper teachingTodoMapper;
    @Resource
    private InstructionalSupervisionMapper instructionalSupervisionMapper;
    @Override
    public Message CountTeacherIdWorkingConditions(int teacherId) {
        List<Schedule> scheduleList = scheduleMapper.selectByTeacherIdAndScheduleStatusAndTimeDayNowInMonth(teacherId);
        HashMap hashMap = new HashMap();
        //查询老师下面所有未完成的课程
        List<Clazz> clazzList = clazzMapper.selectByClassTeacherIdAndStatus01(teacherId);
        int clazzNumber=0;
        int studentNumber=0;
        List<String> studentIds = new LinkedList<>();
        for (Clazz clazz:clazzList){
            clazzNumber++;
            if (clazz.getStudentId()!=null){
                String[] studentIdstoString = clazz.getStudentId().split(";");
                if (studentIdstoString.length>0){
                    for (String studentIdtoString:studentIdstoString){
                        studentIds.add(studentIdtoString);
                    }
                }
            }
        }
        //通过HashSet踢除重复元素
        HashSet hashSet =new HashSet(studentIds);
        studentIds.clear();
        studentIds.addAll(hashSet);
        for (String s:studentIds){
            studentNumber++;
        }
        hashMap.put("clazzNumber",clazzNumber);
        hashMap.put("studentNumber",studentNumber);
        int attendClassNumber=0;

        for (Schedule schedule:scheduleList){
            attendClassNumber=attendClassNumber+schedule.getTimeSkjc();
        }
        System.out.println(scheduleList);
        hashMap.put("attendClassNumber",attendClassNumber);

        hashMap.put("attendClassTimeNumber",attendClassNumber*40/60);
        return Message.success(null).add(hashMap);
    }

    @Override
    public Message countAllTeacherWorkingConditionsInMonth() {
        List<TeacherWorkloadStatistics> returnList = new LinkedList<>();
        List<Staff> staffList = staffMapper.countAllStaffWithJurisdictionInSixAndSevenAndEight();
        for (Staff staff:staffList){
            List<Schedule> scheduleList = scheduleMapper.selectByTeacherIdAndScheduleStatusAndTimeDayNowInMonth(staff.getStaffId());
            if (scheduleList.size()>0){
                List<TeacherWorkloadStatistics> list = coutTeacherWorkloadStatistics(scheduleList);
                for (TeacherWorkloadStatistics teacherWorkloadStatistics:list){
                    returnList.add(teacherWorkloadStatistics);
                }
            }
        }
        return Message.success(null).add(returnList);
    }

    @Override
    public Message countAllStudentPayInMonth() {
        List<PaymentAmountStatistics> list = payMapper.selectAllByPayTimeNowInMonth();
        for (PaymentAmountStatistics record:list){

            record.setPaymentAmountStatisticsStudentName(record.getStudent().getStudentName());
            switch (record.getPaymentAmountStatisticsPayTypeId()){
                case 1:
                    record.setPaymentAmountStatisticsPayType("入训");
                    break;
                case 2:
                    record.setPaymentAmountStatisticsPayType("续费");
                    break;
                case 3:
                    record.setPaymentAmountStatisticsPayType("补费");
                    break;
            }
            TechnicalType technicalType = technicalTypeMapper.selectByPrimaryKey(record.getLesson().getLessonTychnicalTypeId());

            record.setPaymentAmountStatisticsTechnicalTypeName(technicalType.getTechnicalTypeName());
                PurchaseCourseRecords records = purchaseCourseRecordsMapper.selectByPayId(record.getPayId());
                if (records!=null){
                    if (records.getPurchaseClazzId()==null){
                        record.setPaymentAmountStatisticsClassTypeName("暂无绑定课程类型");
                        record.setPaymentAmountStatisticsTeacherName("暂无绑定课程授课老师");
                    }
                    else {
                        Clazz clazz = clazzMapper.selectByPrimaryKey(records.getPurchaseClazzId());
                        Staff staff = staffMapper.selectByPrimaryKey(clazz.getClassTeacherId());
                        record.setPaymentAmountStatisticsClassTypeName(clazz.getClazz_type().getClazzTypeName());
                        record.setPaymentAmountStatisticsTeacherName(staff.getStaffName());
                    }
                }else {
                    record.setPaymentAmountStatisticsClassTypeName("暂无绑定课程类型");
                    record.setPaymentAmountStatisticsTeacherName("暂无绑定课程授课老师");
                }


        }
        return Message.success(null).add(list);
    }

    @Override
    public Message countAllStudentRemainingClassHoursStatisticsInMonth() {
        List<PurchaseCourseRecords> list = purchaseCourseRecordsMapper.selectAll();
        for(PurchaseCourseRecords records:list){
            if (records.getClazz().getClassType()!=null)
            {
                ClazzType clazzType = clazzTypeMapper.selectByPrimaryKey(records.getClazz().getClassType());
                records.setPurchaseClazzTypeName(clazzType.getClazzTypeName());
            }
            if (records.getLesson().getLessonTychnicalTypeId()!=null){
                TechnicalType technicalType =technicalTypeMapper.selectByPrimaryKey(records.getLesson().getLessonTychnicalTypeId());
                records.setPurchasetechnicalTypeName(technicalType.getTechnicalTypeName());
            }
            if (records.getStudent().getStudentTreatPrincipalTeacherId()!=null){
                records.setPurchasePrincipalTeacherName(staffMapper.selectByPrimaryKey(records.getStudent().getStudentTreatPrincipalTeacherId()).getStaffName());
            }
            if (records.getClazz().getClassTeacherId()!=null)
                records.setPurchasePeopleTeacherName(staffMapper.selectByPrimaryKey(records.getClazz().getClassTeacherId()).getStaffName());

        }
        return Message.success(null).add(list);
    }

    /*
    教学管理次数统计权限：0256
    教学记录统计权限：8756
     */
    @Override
    public Message countAllTeachingManagementTimesStatisticsInMonth() {
        List<CountAllTeachingManagementTimesStatistics> list = new LinkedList<>();
        List<Staff> ZeroAndTwoStaffList = staffMapper.countAllStaffWithJurisdictionInZeroAndTwo();
        for (Staff staff:ZeroAndTwoStaffList){
            CountAllTeachingManagementTimesStatistics c = new CountAllTeachingManagementTimesStatistics();
            c.setCountAllTeachingManagementTimesStatisticsTeacherName(staff.getStaffName());
            c.setCountAllTeachingManagementTimesStatisticsTeacherJob(staff.getStaffJob());
            c.setCountAllTeachingManagementTimesStatisticsRecordTimes(0);
            c.setCountAllTeachingManagementTimesStatisticsManagementTimes(countTeachingManagementTimes(staff.getStaffId()));
            list.add(c);
        }
        List<Staff> EightAndSevenStaffList = staffMapper.countAllStaffWithJurisdictionInSevenAndEight();
        for (Staff staff:EightAndSevenStaffList){
            CountAllTeachingManagementTimesStatistics c = new CountAllTeachingManagementTimesStatistics();
            c.setCountAllTeachingManagementTimesStatisticsTeacherName(staff.getStaffName());
            c.setCountAllTeachingManagementTimesStatisticsTeacherJob(staff.getStaffJob());
            c.setCountAllTeachingManagementTimesStatisticsRecordTimes(countTeachingRecordTimes(staff.getStaffId()));
            c.setCountAllTeachingManagementTimesStatisticsManagementTimes(0);
            list.add(c);
        }
        List<Staff> FiveAndSixStaffList = staffMapper.countAllStaffWithJurisdictionInSixAndFive();
        for (Staff staff:FiveAndSixStaffList){
            CountAllTeachingManagementTimesStatistics c = new CountAllTeachingManagementTimesStatistics();
            c.setCountAllTeachingManagementTimesStatisticsTeacherName(staff.getStaffName());
            c.setCountAllTeachingManagementTimesStatisticsTeacherJob(staff.getStaffJob());
            c.setCountAllTeachingManagementTimesStatisticsRecordTimes(countTeachingRecordTimes(staff.getStaffId()));
            c.setCountAllTeachingManagementTimesStatisticsManagementTimes(countTeachingManagementTimes(staff.getStaffId()));
            list.add(c);
        }
        return Message.success(null).add(list);
    }
    /**
     * 统计教学管理次数
     * countAllTeachingManagementTimesStatisticsInMonth（）专用
     * @param staffId
     * @return
     */

    private int countTeachingManagementTimes(int staffId){
        int countNUMBER = 0;
        //统计教学讨论发起次数
        countNUMBER=countNUMBER+teachingDiscussionMapper.countByTeachingDiscussionPostUserIdNowInMonth(staffId);
        //统计教学督导发起次数
        countNUMBER=countNUMBER+instructionalSupervisionMapper.countByInstructionalSupervisionPostUserIdNowInMonth(staffId);
        //统计教学待办发起次数
        countNUMBER=countNUMBER+teachingTodoMapper.countByTeachingTodoPostUserIdNowInMonth(staffId);
        return countNUMBER;
    }
    /**
     * 统计教学记录次数
     * countAllTeachingManagementTimesStatisticsInMonth（）专用
     * @param staffId
     * @return
     */
    private int countTeachingRecordTimes(int staffId){
        int countNUMBER = 0;
        //统计录入学员管理下教学计划次数
        countNUMBER=countNUMBER+plansMapper.CountByPlansTeacherIdNowInMonth(staffId);
        //统计录入学员管理下教学总结次数
        countNUMBER=countNUMBER+teachSummaryMapper.CountByteachPostUserIdNowInMonth(staffId);
        //统计录入班级管理下教学计划次数
        countNUMBER=countNUMBER+plansclazzMapper.CountByPlansclazzPostUserIdNowInMonth(staffId);
        //统计录入班级管理下课程总结次数
        countNUMBER=countNUMBER+courseSummaryMapper.CountByCourseSummaryPostUserIdNowInMonth(staffId);
        return countNUMBER;
    }




    @Override
    public Message countAllStudentStatusStatisticsInMonth() {
        List<CountAllStudentStatus> list = studentMapper.selectStudentIdAndStudentNameAndStudentStatusAndStudentTreatPrincipalTeacherId();
        return Message.success(null).add(list);
    }

    private List<TeacherWorkloadStatistics> coutTeacherWorkloadStatistics(List<Schedule> scheduleList){

        Integer teacherId = scheduleList.get(0).getTeacherId();
        String teacherName = scheduleList.get(0).getStaff().getStaffName();
        //拿到classIds
        List<Integer> classIds = new LinkedList<>();
        classIds.add(scheduleList.get(0).getClassId());
        for (Schedule schedule:scheduleList){
            int flag=0;
            for (Integer classId:classIds){
                if (schedule.getClassId()==classId){
                    flag=1;
                }
            }
            if (flag==0)
                classIds.add(schedule.getClassId());
        }

        List<TeacherWorkloadStatistics> teacherWorkloadStatisticsList = new LinkedList<>();

        for (Integer classId:classIds){
            TeacherWorkloadStatistics workOverTime = new TeacherWorkloadStatistics();
            TeacherWorkloadStatistics normal = new TeacherWorkloadStatistics();
            Clazz clazz = clazzMapper.selectByPrimaryKey(classId);
            TechnicalType technicalType = technicalTypeMapper.selectByPrimaryKey(clazz.getLesson().getLessonTychnicalTypeId());
            //设置工作类别
            workOverTime.setTeacherWorkloadStatisticsWorkTypeName("加班");
            normal.setTeacherWorkloadStatisticsWorkTypeName("正常");
            //设置课程类型
            workOverTime.setTeacherWorkloadStatisticsTClassTypeName(clazz.getClazz_type().getClazzTypeName());
            normal.setTeacherWorkloadStatisticsTClassTypeName(clazz.getClazz_type().getClazzTypeName());
            //设置技术类别
            workOverTime.setTeacherWorkloadStatisticsTechnicalTypeName(technicalType.getTechnicalTypeName());
            normal.setTeacherWorkloadStatisticsTechnicalTypeName(technicalType.getTechnicalTypeName());
            //设置授课时长
            workOverTime.setTeacherWorkloadStatisticsNumberOfLectures(scheduleMapper.sumTimeSkjcByTeacherIdAndClassIdAndScheduleStatusAndScheduleWorkOvertimeAndTimeDayNowInMonth(teacherId,classId,1));
            normal.setTeacherWorkloadStatisticsNumberOfLectures(scheduleMapper.sumTimeSkjcByTeacherIdAndClassIdAndScheduleStatusAndScheduleWorkOvertimeAndTimeDayNowInMonth(teacherId,classId,0));
            //设置教师名字
            workOverTime.setTeacherWorkloadStatisticsTeacherName(teacherName);
            normal.setTeacherWorkloadStatisticsTeacherName(teacherName);

            teacherWorkloadStatisticsList.add(workOverTime);
            teacherWorkloadStatisticsList.add(normal);
        }

        return teacherWorkloadStatisticsList;
    }



}
